package com.tinytoon.hunghoaqua;

import java.util.Random;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;

public class Element {

	public int mType;
	public static final int TYPE_NORMAL = 0;
	public static final int TYPE_FINISH_ANIMATION = 1;
	public static final int TYPE_RED_BORDER = 2;
	public static final int TYPE_SCORE_MINUS = 3;
	public static final int TYPE_SCORE_PLUS = 4;
	public static final int TYPE_BOX_PLUS_1 = 5;
	public static final int TYPE_BOX_PLUS_2 = 6;
	public static final int TYPE_BOX_PLUS_3 = 7;
	public static final int TYPE_BOX_PLUS_4 = 8;
	public static final int TYPE_TIME_PLUS_5 = 9;
	public static final int TYPE_TIME_PLUS_10 = 10;
	public static final int TYPE_TIME_PLUS_20 = 11;

	public int mAnimatioStep = 0;
	public static final int STEP_ANIMATION_START = 0;
	public static final int STEP_ANIMATION_INTERVAL = 5;

	public float mX;
	public float mY;
	public float mSpeedX;
	public float mSpeedY;
	private String mChar;
	private boolean isOutOfScreen = false;
	private Bitmap mBitmap;

	public Element(Resources res, String aChar, float speedX, float speedY) {
		Random rand = new Random();
		mBitmap = GameRes.getBitmap(res, aChar);
		// start from stop of screen
		mX = rand.nextInt((int) Panel.mWidth - mBitmap.getWidth());
		mY = -mBitmap.getHeight();
		this.mSpeedX = speedX;
		this.mSpeedY = speedY;
		mChar = aChar;
		mType = TYPE_NORMAL;
	}

	public int getAnimationStep() {
		return mAnimatioStep;
	}

	public void setAnimationStep(int mExplosionStep) {
		this.mAnimatioStep = mExplosionStep;
	}

	public String getChar() {
		return mChar;
	}

	public void setChar(String type) {
		this.mChar = type;
	}

	public void setY(float mY) {
		this.mY = mY;
	}

	public Bitmap getmBitmap() {
		return mBitmap;
	}

	public void setmBitmap(Bitmap mBitmap) {
		this.mBitmap = mBitmap;
	}

	public void doDraw(Canvas canvas) {
		canvas.drawBitmap(mBitmap, mX, mY, null);
	}

	public void animate() {
		mX += mSpeedX;
		mY += mSpeedY;
		checkBorders();
	}

	private void checkBorders() {
		// left
		if (mX <= 0) {
			mSpeedX = -mSpeedX;
			mX = 0;
		}
		// right
		else if (mX + mBitmap.getWidth() >= Panel.mWidth) {
			mSpeedX = -mSpeedX;
			mX = Panel.mWidth - mBitmap.getWidth();
		}

		// bottom
		if (mY >= Panel.mHeight) {
			isOutOfScreen = true;
		}

		// top
		// if (mY <= 0) {
		// mY = 0;
		// mSpeedY = -mSpeedY;
		// //isOutOfScreen = true;
		// }
	}

	public boolean isOutOfScreen() {
		return isOutOfScreen;
	}

	// check if point(x,y) inside or outside of the icon
	public boolean isContain(int x, int y) {
		int _x = (int) mX;
		int _y = (int) mY;
		if ((x >= _x) && (x <= _x + mBitmap.getWidth()) && (y >= _y)
				&& (y <= _y + mBitmap.getHeight())) {
			return true;
		}
		return false;
	}

	public Bitmap blurBitmap(int color) {
		Bitmap src = mBitmap.copy(mBitmap.getConfig(), true);//mBitmap;
		Bitmap alpha = src.extractAlpha();
		BlurMaskFilter blurMaskFilter = new BlurMaskFilter(2, BlurMaskFilter.Blur.NORMAL);
		Paint paint = new Paint();
		paint.setMaskFilter(blurMaskFilter);
		paint.setColor(color);

		Canvas canvas = new Canvas(src);
		canvas.drawBitmap(alpha, 0, 0, paint);

		return src; 
	}

	public boolean isFruits() {
		return (!mChar.equals("cake") && !mChar.contains("shoe"));
	}

	public String getLowerCaseChar() {
		if (mChar.equals("A"))
			return "a";
		else if (mChar.equals("AA"))
			return "aa";
		else if (mChar.equals("AW"))
			return "aw";
		else if (mChar.equals("B"))
			return "b";
		else if (mChar.equals("C"))
			return "c";
		else if (mChar.equals("D"))
			return "d";
		else if (mChar.equals("DD"))
			return "dd";
		else if (mChar.equals("E"))
			return "e";
		else if (mChar.equals("EE"))
			return "ee";
		else if (mChar.equals("F"))
			return "f";
		else if (mChar.equals("G"))
			return "g";
		else if (mChar.equals("H"))
			return "h";
		else if (mChar.equals("I"))
			return "i";
		else if (mChar.equals("K"))
			return "k";
		else if (mChar.equals("L"))
			return "l";
		else if (mChar.equals("M"))
			return "m";
		else if (mChar.equals("N"))
			return "n";
		else if (mChar.equals("O"))
			return "o";
		else if (mChar.equals("OO"))
			return "oo";
		else if (mChar.equals("OW"))
			return "ow";
		else if (mChar.equals("P"))
			return "p";
		else if (mChar.equals("Q"))
			return "q";
		else if (mChar.equals("R"))
			return "r";
		else if (mChar.equals("S"))
			return "s";
		else if (mChar.equals("T"))
			return "t";
		else if (mChar.equals("U"))
			return "u";
		else if (mChar.equals("UW"))
			return "uw";
		else if (mChar.equals("V"))
			return "v";
		else if (mChar.equals("X"))
			return "x";
		else if (mChar.equals("Y"))
			return "y";

		else if (mChar.equals("G"))
			return "g";
		else if (mChar.equals("H"))
			return "h";
		else if (mChar.equals("I"))
			return "i";
		else if (mChar.equals("J"))
			return "j";
		else if (mChar.equals("K"))
			return "k";
		else
			return mChar;
	}

	public Rect getRect() {
		int left = (int) mX;
		int top = (int) mY;
		int right = (int) (mX + mBitmap.getWidth());
		int bottom = (int) (mY + mBitmap.getHeight());
		return new Rect(left, top, right, bottom);
	}
}
